//! # 配置中心统一错误处理
//!
//! 提供配置中心的统一错误类型定义和错误处理机制，
//! 实现错误类型的统一管理和转换。

use std::fmt;

use crate::core::*;

/// 配置中心统一错误类型
#[derive(Debug)]
pub enum ConfigCenterError {
    /// 核心接口错误
    CoreError(Box<dyn std::error::Error + Send + Sync>),
    
    /// 配置服务器错误
    #[cfg(feature = "server")]
    ServerError(crate::server::ConfigurationServerError),
    
    /// 配置客户端错误
    #[cfg(feature = "client")]
    ClientError(crate::client::ConfigurationClientError),
    
    /// 存储错误
    StorageError(String),
    
    /// 加密错误
    EncryptionError(String),
    
    /// 网络错误
    NetworkError(String),
    
    /// 认证错误
    AuthenticationError(String),
    
    /// 权限错误
    AuthorizationError(String),
    
    /// 配置验证错误
    ValidationError(String),
    
    /// 超时错误
    TimeoutError(String),
    
    /// 内部错误
    Internal(String),
    
    /// 未找到错误
    NotFound(String),
    
    /// 配置广播错误
    #[cfg(feature = "broadcast")]
    BroadcastError(String),
    
    /// 解析错误
    ParseError(String),
    
    /// 未知错误
    UnknownError(String),
}

impl fmt::Display for ConfigCenterError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ConfigCenterError::CoreError(e) => write!(f, "核心接口错误: {}", e),
            #[cfg(feature = "server")]
            ConfigCenterError::ServerError(e) => write!(f, "服务器错误: {}", e),
            #[cfg(feature = "client")]
            ConfigCenterError::ClientError(e) => write!(f, "客户端错误: {}", e),
            ConfigCenterError::StorageError(e) => write!(f, "存储错误: {}", e),
            ConfigCenterError::EncryptionError(e) => write!(f, "加密错误: {}", e),
            ConfigCenterError::NetworkError(e) => write!(f, "网络错误: {}", e),
            ConfigCenterError::AuthenticationError(e) => write!(f, "认证错误: {}", e),
            ConfigCenterError::AuthorizationError(e) => write!(f, "权限错误: {}", e),
            ConfigCenterError::ValidationError(e) => write!(f, "验证错误: {}", e),
            ConfigCenterError::TimeoutError(e) => write!(f, "超时错误: {}", e),
            ConfigCenterError::Internal(e) => write!(f, "内部错误: {}", e),
            ConfigCenterError::NotFound(e) => write!(f, "未找到: {}", e),
            #[cfg(feature = "broadcast")]
            ConfigCenterError::BroadcastError(e) => write!(f, "配置广播错误: {}", e),
            ConfigCenterError::ParseError(e) => write!(f, "解析错误: {}", e),
            ConfigCenterError::UnknownError(e) => write!(f, "未知错误: {}", e),
        }
    }
}

impl std::error::Error for ConfigCenterError {}

impl From<Box<dyn std::error::Error + Send + Sync>> for ConfigCenterError {
    fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
        ConfigCenterError::CoreError(err)
    }
}

#[cfg(feature = "server")]
impl From<crate::server::ConfigurationServerError> for ConfigCenterError {
    fn from(err: crate::server::ConfigurationServerError) -> Self {
        ConfigCenterError::ServerError(err)
    }
}

#[cfg(feature = "client")]
impl From<crate::client::ConfigurationClientError> for ConfigCenterError {
    fn from(err: crate::client::ConfigurationClientError) -> Self {
        ConfigCenterError::ClientError(err)
    }
}

/// 配置中心结果类型
pub type ConfigCenterResult<T> = Result<T, ConfigCenterError>;

/// 配置结果类型别名（用于配置广播功能）
pub type ConfigResult<T> = Result<T, ConfigCenterError>;

/// 配置错误类型别名（用于配置广播功能）
pub type ConfigError = ConfigCenterError;

/// 错误转换trait
pub trait IntoConfigCenterError<T> {
    /// 转换为配置中心错误
    fn into_config_center_error(self) -> ConfigCenterResult<T>;
}

impl<T> IntoConfigCenterError<T> for ConfigurationResult<T> {
    fn into_config_center_error(self) -> ConfigCenterResult<T> {
        match self {
            Ok(value) => Ok(value),
            Err(e) => Err(ConfigCenterError::CoreError(e)),
        }
    }
}

#[cfg(feature = "server")]
impl<T> IntoConfigCenterError<T> for crate::server::ServerResult<T> {
    fn into_config_center_error(self) -> ConfigCenterResult<T> {
        match self {
            Ok(value) => Ok(value),
            Err(e) => Err(ConfigCenterError::ServerError(e)),
        }
    }
}

#[cfg(feature = "client")]
impl<T> IntoConfigCenterError<T> for crate::client::ClientResult<T> {
    fn into_config_center_error(self) -> ConfigCenterResult<T> {
        match self {
            Ok(value) => Ok(value),
            Err(e) => Err(ConfigCenterError::ClientError(e)),
        }
    }
}